package com.example.demo.controller;

import com.example.demo.model.*;
import com.example.demo.model.vo.InquiryAddVo;
import com.example.demo.model.vo.PartsCerteriaVo;
import com.example.demo.model.vo.SupplierCerteriaVo;
import com.example.demo.service.InquiryService;
import com.example.demo.service.PartsService;
import com.example.demo.service.SupplierService;
import com.example.demo.util.AllRecords;
import com.example.demo.util.Result;
import com.example.demo.util.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;


/**
 * Created by zkq19 on 2018/7/1.
 */
@RestController
public class InquiryController {
    @Autowired
    private PartsService partsService;
    @Autowired
    private SupplierService supplierService;
    @Autowired
    private InquiryService inquiryService;

    @PostMapping("/inquiry/parts/find")
    public Result<AllRecords<Parts>> findParts(@RequestBody PartsCerteriaVo partsCerteriaVo){

        return partsService.queryPage(partsCerteriaVo);
    }
    @PostMapping("/inquiry/supplier/find")
    public Result<AllRecords<Supplier>> findSupplier(@RequestBody SupplierCerteriaVo supplierCerteriaVo){

        return supplierService.queryPage(supplierCerteriaVo);
    }

    @PostMapping("/inquiry/add")
    public Result<InquiryAddVo> addInquiry(@RequestBody @Valid InquiryAddVo inquiryAddVo,BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            System.out.println(bindingResult.getFieldError().getDefaultMessage());
            return ResultVO.error(1,bindingResult.getFieldError().getDefaultMessage());
        }
        return inquiryService.addInquiry(inquiryAddVo);
    }
    @PostMapping("/inquiry/update")
    public Result<InquiryAddVo> updateInquiry(@RequestBody @Valid InquiryAddVo inquiryAddVo,BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            System.out.println(bindingResult.getFieldError().getDefaultMessage());
            return ResultVO.error(1,bindingResult.getFieldError().getDefaultMessage());
        }
        return inquiryService.updateInquiry(inquiryAddVo);
    }
}
